public class Main {
    public int longestOnes(int[] nums, int k) {
        int sum = 0,len = 0;
        for (int left = 0,right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum + k < right-left+1) {
                sum -= nums[left++];
            }
            len = Math.max(len,right-left+1);
        }
        return len;
    }
}
